from Common.HITPropertiesTemplate import HITPropertiesTemplate, \
    HITPropertiesTemplateEncoder
from Controller.BaseController import BaseController
from Utility import ParameterKey
#from Utility import ViewName
from django.utils import simplejson
#from urllib import quote
#import Configuration

class GetHITPropertiesTemplateController(BaseController):
    def get(self):
        """
        Load HIT Properties object according to user select
        It take a query string "hpid"
        """
        u = self.GetCurrentUser()
        if u is None:
            self.response.write("Click here to <a href='/login/re?/create/hit'>login</a> now.")
            return

        hp_id = self.request.get(ParameterKey.HIT_Properties_Template_ID)
        if hp_id is not None:
            # Load HIT Properties object according to this id
            # serialize HIT Properties Template object to JSON string
            hitPropertiesTemplate = HITPropertiesTemplate.get(hp_id)
            if bool(self.request.get(ParameterKey.RENDERER_JSON)):
                #render json string when user make selection from the name dropdown to template name
                template_values = {'hitPropertiesTemplate': hitPropertiesTemplate, 'reward': hitPropertiesTemplate.rewards[0].FormattedPrice}
                self.response.out.write(simplejson.dumps(template_values, cls=HITPropertiesTemplateEncoder))
            else:
                template_values = {'HITPropertiesTemplates': u.Ref_Company.HITPropertiesTemplates, 'hitPropertiesTemplate': hitPropertiesTemplate, 'reward': hitPropertiesTemplate.Rewards[0]}
                self.Render("templates/HITPropertiesSetting", template_values)
        else:
            # return error message
            self.response.write("You did not enter right template id")